package com.suzhiliang.springbootlesson.login;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 */
public class SessionInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        //登录界面和获取token不拦截
        if((request.getRequestURI().equals("/login/login"))
                || (request.getRequestURI().equals("/login/login_view")) ||
                (request.getRequestURI().contains("/oauth")) ||
                (request.getRequestURI().contains("/error"))
                || (request.getRequestURI().contains("/cors"))){
            return true;
        }
        //从session中获取
        Object user = request.getSession().getAttribute("session_user");
        if (user==null){
            response.sendRedirect("/login/login_view");
            return false;
        }
        return true;
    }
}
